home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
forth
/
amiga
/
amigaker.arc
/
files.txt
< prev
next >
Wrap
File List
|
1987-12-30
|
3KB
|
124 lines
\ files.examples 23may87pja
\ Some (loadable) examples of file handling
\ set the environment
decimal
only forth also definitions
\ Showing a file on the screen.
\ Usage: typef <name>
: typef \ name
from
cr
begin stdbuffer 250 [ dos ] gets dup
while type repeat
2drop close-files ;
\ The standard output buffer can be used as scratch buffer
\ because normally it is used for emit and multiple emits
\ of the same character.
\ gets requires a buffer address and a length, fills it with
\ a line and returns the length, zero it the end of the file
\ is reached.
\ Closing the file causes no harm, if only the 'from' file
\ should be closed, use: 'in-file file-close'
\ Linecount, count the number of lines in a file.
\ Usage: lc <name> |.|
: lc \ name (s -- n ) returns number of lines in a file.
from
0 begin stdbuffer 250 [ dos ] gets nip
while 1+ repeat
in-file file-close ;
\ Putting the 'variable count' on the stack is simple, but the stack
\ must be tightly controlled. By getting rid of the buffer address and
\ then testing for null, the stack is emptied except for the count.
\ After the routine is finished, the count can be shown.
\ Show a file with linecount
\ Usage: typefn <name>
: typefn \ name
from
cr
0 begin stdbuffer 4+ 250 [ dos ] gets dup
while rot 1+
dup 4 .r -rot
ascii : emit space
type
repeat
2drop drop close-files ;
\ The buffer must be moved up slightly in order for the emit of the
\ colon not to interfere with the line from the file.
\ The word space also uses the stdbuffer.
\ Printing a number doesn't interfere, it uses 'pad' which floats
\ above 'here'.
\ Showing a file, using getc
\ Usage: showc <name>
: showc \ name
from
cr
begin [ dos ] getc dup 1+ 0<> while emit repeat
drop close-files ;
\ Counting characters, using getc
\ Usage: countc <name>
: countc \ name
from
cr
0
begin [ dos ] getc 1+ 0<>
while 1+ repeat
close-files . ;
\ Counting lines, using getc
: countl \ name
from
cr
0
begin [ dos ] getc dup 1+ 0<>
while 10 = if 1+ then
repeat
drop close-files . ;
\ Typing only the lines with a character in it
: selectlines \ name (s char -- )
from
cr
begin [ dos ] stdbuffer 4+ 250 gets dup
while 3dup rot scan nip if type else 2drop then
repeat
2drop drop close-files ;
\ Showing only the lines in a file starting with the character *
\ Usefull in seeing the definitions in a file
: words-in \ name
from cr
begin stdbuffer 4+ 250 [ dos ] gets dup
while over c@ ascii * = if type else 2drop then
key? if key control c = if exit then
key control c = if exit then then
repeat
2drop close-files ;